package com.ibeeking.found.msg.rest.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ibeeking.found.msg.rest.service.*;
import com.ibeeking.found.msg.service.common.vo.EmailConfigVO;
import com.ibeeking.found.msg.service.common.vo.EmailTemplateVO;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.msg.email.core.entity.EmailMessage;
import com.ibeeking.nematos.msg.email.core.service.EmailService;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrSubstitutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SendEmailServiceImpl
 * @Description 发送模板邮件
 * @Author ibeeking
 * @Date 2020-12-10 9:21
 **/
@Service
public class SendEmailServiceImpl implements ISendEmailService {

    @Resource
    private IEmailConfigService emailConfigServiceImpl;

    @Resource
    private IEmailTemplateService emailConfigTemplateServiceImpl;

    @Resource
    private IOpenService openServiceImpl;

    @Override
    public Boolean sendSimpleEmail(String tenantId, List<String> toUser, String subject, String content) throws MessagingException, GeneralSecurityException {
        EmailConfigVO emailConfigVO = emailConfigServiceImpl.viewDefaultEmailConfig();

        EmailMessage message = new EmailMessage();
        message.setSubject(subject);
        message.setTo(toUser.toArray(new String[toUser.size()]));
        message.setContent(content);
        EmailService emailService = openServiceImpl.getEmailService(Long.valueOf(tenantId));
        LogUtils.info(">>>>>>>sendSimpleEmail:{}", JsonUtils.toJsonStr(message));
        emailService.sendEmail(message);
        return true;
    }

    @Override
    public Boolean sendTemplateEmail(String tenantId, String templateCode, List<String> toUser, Map<String, String> templateData) throws MessagingException, GeneralSecurityException {

        EmailTemplateVO emailTemplateVO = emailConfigTemplateServiceImpl.queryTemplateByCode(templateCode);
        if (ObjectUtil.isNull(emailTemplateVO)) {
            throw new BusinessException("邮件模板查询失败 templateCode:" + templateCode);
        }

        String subject = emailTemplateVO.getTemplateSubject();
        String templateContent = emailTemplateVO.getTemplateContent();
        if (StringUtils.isBlank(templateContent)) {
            throw new BusinessException("邮件发送失败 模板内容为空 templateCode:" + templateCode);
        }

        // 组装模板内容 模板格式参考 "<p>您好，${name}，您的工号是${idNumber},您的年龄是${age}</p>";
        StrSubstitutor strSubstitutor = new StrSubstitutor(templateData);
        String content = strSubstitutor.replace(templateContent);
        LogUtils.info(">>>>>>>sendSimpleEmail content:{}", content);
        return sendSimpleEmail(tenantId, toUser, subject, content);
    }

}
